<!--
    author:yangwenbin
    time:2023/6/29
    tips:属性：1.moduleParam:传id 以及宽高的对象 2.optionData:传渲染数据
 -->
<template>
  <view class="echart-box">
    <!-- #ifdef APP-PLUS || H5 -->
    <view :id="moduleParam.id" :style="{ width: moduleParam.width, height: moduleParam.height }" :prop="optionData"
      :module-param-prop="moduleParam" :change:module-param-prop="echarts.moduleParamUp"
      :change:prop="echarts.updateEcharts" class="echarts" @click="echarts.onClick" />
    <!-- #endif -->
    <!-- #ifndef APP-PLUS || H5 -->
    <view>非 APP、H5 环境不支持</view>
    <!-- #endif -->
  </view>
</template>
<script>
export default {
  props: {
    moduleParam: {
      type: Object,
      default: () => {
        return {
          id: "myCharts",
          height: "100vw",
          width: "100vh"
        }
      }
    },
    optionData: {
      type: Object,
      default: () => { }
    },
  },
  data() {
    return {}
  },

  methods: {
    onViewClick(options) {
      this.$emit("getClickData", options)
    }
  }
}
</script>
<script module="echarts" lang="renderjs">
    let myChart
    export default {
        data() {
            return {
                clickData: null
            }
        },
        watch: {
          optionData: {
            handler() {
              console.log(1);
              this.initEcharts()
            },
            deep: true
          }
        },
        mounted() {
            if (typeof window.echarts === 'function') {
                this.initEcharts()
            } else {
                // 动态引入较大类库避免影响页面展示
                const script = document.createElement('script')
                // view 层的页面运行在 www 根目录，其相对路径相对于 www 计算
                script.src = 'static/echarts/echarts.js'
                script.onload = this.initEcharts.bind(this)
                document.head.appendChild(script)
            }
        },
        methods: {
            initEcharts() {
              if(myChart){
                myChart.dispose();
                myChart = null;
              }
                myChart = echarts.init(document.getElementById(this.moduleParam.id), "echartsConfig")
                // 观测更新的数据在 view 层可以直接访问到
                myChart.setOption(this.optionData)
                // 点击传参
                myChart.on('click', params => {
                    this.clickData = params
                })
            },
            updateEcharts(newValue, oldValue, ownerInstance, instance) {
                // 监听 service 层数据变更
                try {
                    myChart = echarts.init(document.getElementById(this.moduleParam.id), "echartsConfig")
                    myChart.setOption(newValue)
                } catch (error) {}
            },
            moduleParamUp(newvalue, oldvalue) {},
            onClick(event, ownerInstance) {
                ownerInstance.callMethod('onViewClick', {
                    value: this.clickData.value,
                    name: this.clickData.name,
                    dataIndex: this.clickData.dataIndex,
                    seriesName: this.clickData.seriesName
                })
            }
        }
    }
</script>
<style>
.content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
</style>
